﻿using System.Web.Mvc;
using WebUI.Areas.Bots.Models;
using Services;
using System.Collections.Generic;
using System.Linq;
using Entities;
using System;

namespace WebUI.Areas.Bots.Controllers
{
    public partial class BotFiltersController : BaseController
    {
        #region  Member Vairables

        readonly IBotService _botService;
        readonly ICSVService _csvService;

        #endregion

        #region Constructor

        public BotFiltersController(ISessionHelper sessionHelper, IBotService botService, ICSVService csvService)
            : base(sessionHelper)
        {
            this._botService = botService;
            this._csvService = csvService;
        }

        #endregion

        #region Action Methods

        public virtual ViewResult Index()
        {
            BotFilters botFilters = new BotFilters
            {
                MinOpsCount = _sessionHelper.Bot.MinOpsCount,
                MinFileSize = _sessionHelper.Bot.MinFileSize,
                Path = _sessionHelper.Bot.Path,
                FileDirectoryName = _sessionHelper.Bot.FileDirectoryName,
                FileExtension = _sessionHelper.Bot.FileExtension,
                AllHours = _sessionHelper.Bot.AllHours,
                PeriodHours = _csvService.ConstructDelimitedString(_sessionHelper.Bot.BotPeriodHours.Select(h => h.Hour.ToString()))
            };

            if (_sessionHelper.Bot.SelectedSize.HasValue)
            {
                botFilters.SelectedSize = _sessionHelper.Bot.SelectedSize.Value;
            }

            return View(botFilters);
        }

        [HttpPost]
        public virtual ActionResult Save(BotFilters model)
        {
            if (ModelState.IsValid)
            {
                _sessionHelper.Bot.MinOpsCount = model.MinOpsCount;
                _sessionHelper.Bot.MinFileSize = model.MinFileSize;
                _sessionHelper.Bot.Path = model.Path;
                _sessionHelper.Bot.FileDirectoryName = model.FileDirectoryName;
                _sessionHelper.Bot.FileExtension = model.FileExtension;
                _sessionHelper.Bot.AllHours = model.AllHours;
                _sessionHelper.Bot.SelectedSize = model.SelectedSize;

                var selectedHours = string.IsNullOrEmpty(model.PeriodHours) ? new string[] { } : model.PeriodHours.Split(new char[1] { ',' });
                _sessionHelper.Bot.BotPeriodHours = new TrackableCollection<BotPeriodHour>();

                foreach (var hour in selectedHours)
                {
                    _sessionHelper.Bot.BotPeriodHours.Add(new BotPeriodHour { Hour = int.Parse(hour) });
                }

                IDictionary<string, string> validationDictionary = _botService.ValidateBotFilterStep(_sessionHelper.Bot);

                foreach (var entry in validationDictionary.Values)
                {
                    ModelState.AddModelError(string.Empty, entry);
                }

                if (ModelState.IsValid)
                {
                    return RedirectToRoute(new { action = "Index", controller = MVC.Bots.Operations.Name, step = MVC.Bots.Operations.Name });
                }
            }

            return View(MVC.Bots.BotFilters.ActionNames.Index, model);
        }

        #endregion
    }
}